/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.mgt.server.dal.repository.power;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.repository.JoinCrudRepository;
import com.iwindplus.mgt.domain.dto.power.PositionRoleDTO;
import com.iwindplus.mgt.server.dal.mapper.power.PositionRoleMapper;
import com.iwindplus.mgt.server.dal.model.power.PositionRoleDO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * 职位角色关系聚合问层接口类.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Repository
public class PositionRoleRepository extends JoinCrudRepository<PositionRoleMapper, PositionRoleDO> {

    /**
     * 添加.
     *
     * @param entity 对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(PositionRoleDTO entity) {
        List<PositionRoleDO> entities = new ArrayList<>(10);
        final PositionRoleDO model = BeanUtil.copyProperties(entity, PositionRoleDO.class);
        this.buildParam(model, entities);
        if (CollUtil.isNotEmpty(entities)) {
            super.saveBatch(entities, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 添加.
     *
     * @param positionId 职位主键
     * @param roleIds    角色主键集合
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchRole(Long positionId, Set<Long> roleIds) {
        if (Objects.isNull(positionId) || CollUtil.isEmpty(roleIds)) {
            return Boolean.FALSE;
        }
        List<PositionRoleDO> entities = new ArrayList<>(10);
        roleIds.stream().forEach(roleId -> {
            PositionRoleDO entity = PositionRoleDO.builder()
                .positionId(positionId)
                .roleId(roleId)
                .build();
            this.buildParam(entity, entities);
        });
        if (CollUtil.isNotEmpty(entities)) {
            super.saveBatch(entities, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 添加.
     *
     * @param roleId      角色主键
     * @param positionIds 职位主键集合
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchPosition(Long roleId, Set<Long> positionIds) {
        if (Objects.isNull(roleId) || CollUtil.isEmpty(positionIds)) {
            return Boolean.FALSE;
        }
        List<PositionRoleDO> entities = new ArrayList<>(10);
        positionIds.stream().forEach(positionId -> {
            PositionRoleDO entity = PositionRoleDO.builder()
                .positionId(positionId)
                .roleId(roleId)
                .build();
            this.buildParam(entity, entities);
        });
        if (CollUtil.isNotEmpty(entities)) {
            super.saveBatch(entities, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 添加.
     *
     * @param entities 对象集合
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<PositionRoleDTO> entities) {
        if (CollUtil.isEmpty(entities)) {
            return Boolean.FALSE;
        }
        List<PositionRoleDO> params = new ArrayList<>(10);
        entities.stream().forEach(entity -> {
            PositionRoleDO model = BeanUtil.copyProperties(entity, PositionRoleDO.class);
            this.buildParam(model, params);
        });
        if (CollUtil.isNotEmpty(params)) {
            super.saveBatch(params, DEFAULT_BATCH_SIZE);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 编辑.
     *
     * @param positionId 职位主键
     * @param roleIds    角色主键集合
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean editBatchRole(Long positionId, Set<Long> roleIds) {
        if (Objects.isNull(positionId)) {
            return Boolean.FALSE;
        }
        super.getBaseMapper().deleteByPositionIds(Arrays.asList(positionId));
        this.saveBatchRole(positionId, roleIds);
        return Boolean.TRUE;
    }

    /**
     * 编辑.
     *
     * @param roleId      角色主键
     * @param positionIds 职位主键集合
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean editBatchPosition(Long roleId, Set<Long> positionIds) {
        if (Objects.isNull(roleId)) {
            return Boolean.FALSE;
        }
        super.getBaseMapper().deleteByRoleIds(Arrays.asList(roleId));
        this.saveBatchPosition(roleId, positionIds);
        return Boolean.TRUE;
    }

    private void buildParam(PositionRoleDO entity, List<PositionRoleDO> entities) {
        long count = super.count(Wrappers.lambdaQuery(PositionRoleDO.class)
            .eq(PositionRoleDO::getPositionId, entity.getPositionId())
            .eq(PositionRoleDO::getRoleId, entity.getRoleId()));
        if (!SqlHelper.retBool(count)) {
            entities.add(entity);
        }
    }
}
